#!../common/bats/bin/bats
# -*-sh-*-

load ./common/test_helper_functions
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

# Test how many computes we have available presently

which koomie_cf >& /dev/null 
if [ $? -ne 0 ]; then 
    ERROR "koomie_cf executable not found - please verify losf is installed."
fi

num_computes=`koomie_cf -x "c[0-9]+\b" uptime | grep "load average" | wc -l`
if [ "$num_computes" -lt 0 ];then
    ERROR "Test requires at least 1 available compute node"
fi
sms_kernel=`uname -r`

@test "[BOS] OS distribution matches $BOS_RELEASE ($num_computes active computes) " {
    skip "disable BOS_RELEASE check"
    koomie_cf -x "c[0-9]+\b" ls -l /etc/$BOS_RELEASE | awk '{print $10}' >& .cmd_output || exit 1
    local num_matches=`grep $BOS_RELEASE .cmd_output | wc -l` || exit 1

    if [ "$num_matches" -ne "$num_computes" ];then
	flunk
    fi
}

@test "[BOS] consistent kernel ($num_computes active computes) " {
    koomie_cf -x "c[0-9]+\b" uname -r >& .cmd_output || exit 1

    for kernel in `cat .cmd_output | awk '{print $2}'`; do
	assert_equal $kernel $sms_kernel
    done

    rm -f .cmd_output
}

@test "[BOS] increased locked memory limits " {
    [[ "$ARCH" == "aarch64" ]] && skip "Skipping memlock settings for ARCH=$ARCH"

    koomie_cf -x "c[0-9]+\b" prlimit -l -o SOFT | grep -v SOFT >& .cmd_output || exit 1

    for limit in `cat .cmd_output | awk '{print $2}'`; do
	assert_equal $limit unlimited
    done

    rm -f .cmd_output

    koomie_cf -x "c[0-9]+\b" prlimit -l -o HARD | grep -v HARD >& .cmd_output || exit 1

    for limit in `cat .cmd_output | awk '{print $2}'`; do
	assert_equal $limit unlimited
    done

    rm -f .cmd_output

}

@test "[BOS] syslog forwarding " {
    test_string=$(head /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1)
    koomie_cf -x "c[0-9]+\b" logger $test_string
    if [ grep $test_string /var/log/messages | wc -l != $num_computes ];then
        for n in `seq 1 $num_computes`; do
            if [ ! grep $test_string | grep c$n ];then
                echo "No syslog forward from c$n."
            fi
        done
        flunk
    fi
}
